#include "stdafx.h"
#include "SpriteInfo.h"
#include "DispositifD3D.h"
#include "RessourceManager.h"
#include "Texture.h"
#include "InputKeyMouse.h"
#include "CarHuman.h"
#include "PhysicEngine.h"
#include "util.h"


using namespace UtilitairesDX;

namespace PM3D
{
    const float SpriteInfo::DEFAULT_ANGLE = 55.0f;

    SpriteInfo::SpriteInfo(const std::wstring& filename_in, Dispositif* d3dDevice, 
        float x, float y,float cx, float cy, SpriteType type, float opacity)
        : type_(type), prevAngle_(DEFAULT_ANGLE)
    {
        RessourceManager<Texture>& textureMgr = RessourceManager<Texture>::getInstance();
        texture_ = textureMgr.getNewRessource(filename_in,d3dDevice,0);

        DispositifD3D* d3dDeviceD3D = dynamic_cast<DispositifD3D*>(d3dDevice);
        IDirect3DDevice9* d3dDevicel = d3dDeviceD3D->getD3DDevice();

        D3DXCreateSprite(d3dDevicel,&sprite_);

        D3DXMatrixIdentity(&matWorld_);
        D3DCOLOR();
        // Decalage du sprite a l`ecran
        offset_ = D3DXVECTOR3(x,y,0.0f);
        position_ = D3DXVECTOR3(0.0f,0.0f,0.0f);

        // Centre du sprite
        center_ = D3DXVECTOR3(cx,cy,0.0f);

        opacity_ = static_cast<DWORD>(opacity * 0xFF);
        opacity_ <<= 24;

        opacity_ |= 0x00FFFFFF;
    }

    SpriteInfo::~SpriteInfo()
    {
        DXRelacher(sprite_); 
    }

    void SpriteInfo::translate(D3DXVECTOR3 offset)
    {
        offset_ += offset;
    }

    void SpriteInfo::draw()
    {
        // Position sprite sur l'ecran
        matWorld_._41 = offset_.x;
        matWorld_._42 = offset_.y;
        matWorld_._43 = offset_.z;
        matWorld_._44 = 1.0f;

        sprite_->SetTransform(&matWorld_);

        sprite_->Begin(D3DXSPRITE_ALPHABLEND);
        sprite_->Draw(texture_->getD3DTexture(),
            NULL,
            &center_,
            &position_,
            opacity_);
        sprite_->End();	
    }

    void SpriteInfo::rotateNeedle()
    {
        PHYSIC::PhysicEngine& moteurPhysique = PHYSIC::PhysicEngine::getInstance();
        PHYSIC::CarHuman* tempCar;		

        tempCar = PHYSIC::PhysicEngine::getInstance().getAutoHuman();

        // Recuperation velocite du vehicle
        D3DXVECTOR3 velo = tempCar->getLinearVelocity();
        velo.z = 0;

        // Calcule vitesse du vehicle
        float vitesse = D3DXVec3Length(&velo); 
        //std::cout<<"Vitesse: "<<vitesse<<"\n";

        float angle;
        angle = prevAngle_;

        //angle =  55.0f   vitesseMin == 0
        //angle =  210.0f  vitesseMax == 90
        // Calcule angle de l'aiguille, si vitesse < 80, sinon angle egale a l'angle precedent
        if(vitesse < 90.0f)
        {
            angle = DEFAULT_ANGLE + ((vitesse/90.0f) * 156.0f);
        }

        if(vitesse > 95.0f)
        {
            //vitesse = 90 quand angle = 210.0f;
            angle = 210.0f;
        }

        // Sauvegarde angle precedente
        prevAngle_ = angle;

        D3DXMATRIX temp;
        D3DXMATRIX rotZ;
        D3DXMatrixIdentity(&rotZ);
        D3DXMatrixIdentity(&temp);

        if(type_ == DYNAMIC)
        {
            D3DXMatrixRotationZ(&rotZ,D3DXToRadian(angle));
        }
        temp*= rotZ;

        // Mis a jour matrice world
        matWorld_ = temp;
    }
}       // namespace PM3D
